<template>
 <div>
	 <el-dialog title="盘库调整" class="my-dialog-class" width="70%" height="550"  :append-to-body="appendToBody"
		:visible.sync="dialogVisible"     :before-close="handleClose"   @opened="findDetailPage" >
			<div class="gk-layout-main" style="height:470px">
				<el-form :model="selectStock" label-width="90px" ref="dataForm1" :size="global.gSize"  label-position="right">
					<el-row :gutter="24">
						<el-col :span="8">
							<el-form-item label="托盘条码" prop="boxCode">
								<el-input v-model="selectStock.boxCode"  auto-complete="off" style="width: 100%;"   :disabled="selectStock.id>0"/>	
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<s-dict name="wh"  v-model="selectStock.wh"/>
						</el-col>		
					</el-row> 
				</el-form>
				<my-table :data="detailList"    v-loading="loading" :key="timer" >
					<el-table-column :label="$t('action.desc')" width="50" fixed="left"  header-align="center" align="center">
						<template slot-scope="scope">   {{scope.$index+1}}   </template>
					</el-table-column> 
					<Column  prop="itemId" label="物料名称"   :formatter= "itemFilter" /> 
					<Column label="数量" prop="countDb"   :formatter= "countFormat" /> 
					<Column   prop="status" label="状态"    :formatter= "selectionFormat" />
					<Column   prop="createTime" label="入库时间"     :formatter= "dateFormatDay" />
					<el-table-column    :label="$t('action.operation')" width="120">
						<template slot-scope="scope">
							<kt-button   label="修改库存"  perms="core:coreStock:showDetail"       @click="editRow(scope,true)" />
						</template>
					</el-table-column>
				</my-table>
		 </div>
		 <div slot="footer"  class="dialog-footer">
		 	<el-button :size="global.gSize"   type="primary"   :loading="editLoading"  @click.native="addDet()">盘盈</el-button>
			<el-button :size="global.gSize"  @click.native="handleClose()">{{$t('action.cancel')}}</el-button>
		</div>
		</el-dialog>

		<el-dialog title="盘亏"   width="300px"  :visible.sync="pkLessDialogVisible" 
		:before-close="handleClosePkLess"   class="my-dialog-class"  :close-on-click-modal="true">
			<el-form :model="pkLessForm"  label-width="80px" ref="pkLessForm"  :rules="dataFormRules"  :size="global.gSize"   label-position="right">
				<el-row>  
					<el-col :span="24">
						<el-form-item label="剩余数量"  prop="countDb">
							<el-input type="text" v-model="pkLessForm.countDb"   ></el-input>
						</el-form-item> 
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="global.gSize"  @click.native="pkLessDialogVisible=false">{{$t('action.cancel')}}</el-button>
				<el-button :size="global.gSize" type="primary"   :loading="editLoading"     @click.native="pkLessSubmitForm">{{$t('action.submit')}}</el-button>
			</div>
		</el-dialog>
 		<core-stock-Det   :detDialogVisible="pkMoreVisible" :editCount=true :editItem=true :editBatch=true     @myClose="detClose" @onSubmitForm="pkMore"/>
	</div>
</template>
<script>
import KtButton from "@/views/Core/KtButton"
import MyTable from "@/views/Core/MyTable"
import Column from "@/views/Core/Column"
import { format,formatDay } from "@/utils/datetime"
import {  dictsFormat ,getItemName} from "@/utils/myUtil" 
import CoreStockDet from "@/dialog/CoreStockDet" 
import { validatorNumber } from "@/utils/validate"
import SDict from "@/components/Kj/SDict"
export default {
	components:{
		MyTable,
		KtButton,
		Column,
		CoreStockDet,SDict
	},
	props: {
			appendToBody:{
				type:Boolean,
				default:true,
			},
			dialogVisible: false,
			row:{type:Object},
			pkId:0
	},
	data() { 
		return { 
	 		timer:new Date().getTime() , //声明timer  
			 pkLessDialogVisible:false,
			loading: false, 
		    pkMoreVisible:false,
			selectStock: {},
			detailList:[],
			editLoading: false,	 
			dataFormRules: { 
					countDb: [{ required: true, trigger: 'blur',validator: validatorNumber }]
            },
			pkLessForm:{},
			selectItem:false 
		}
	},
	methods: {
		pkLessSubmitForm: function () {
			this.$refs.pkLessForm.validate((valid) => {
				if (valid) {
					this.$confirm(this.$t('action.isConfirm'), this.$t('action.tips'), {}).then(() => {
						this.editLoading = true
						let params = Object.assign({}, this.pkLessForm)
						this.$api.coreStockDet.pkLess(params).then((res) => {
							this.editLoading = false
							if (res.code == 200) {
								this.$message({ message: this.$t('action.operateSucess'), type: 'success' })
								this.pkLessDialogVisible=false;
								this.findDetailPage();
							} else {
								this.$message({ message: this.$t('action.operateFail') + res.msg, type: 'error' })
							}
						
						})
					})
				}
			})
		},
		pkMore :function (data) {
			 data.pid=this.selectStock.id;
			 data.status=8
			 data.doiType=1
			 data.pkId=this.pkId
			 
			 
			 this.$api.coreStockDet.saveStockDet(data).then((res) => {
				if (res.code == 200) {
					this.$message({ message: this.$t('action.operateSucess'), type: 'success' })
					this.detClose()
				} else {
					this.$message({ message: this.$t('action.operateFail') + res.msg, type: 'error' })
				}
			
			})
		},
		addDet:function(){
			this.pkMoreVisible=true;
		},
		detClose:function(det){
			this.findDetailPage();
			this.pkMoreVisible=false;
		},
 
		countFormat: function (row, column, cellValue, index) {
			return cellValue +dictsFormat("units",row.unit)
		},

		itemFilter: function (row, column, cellValue, index) { 
			return getItemName(cellValue)
		},
	 				
	    // 显示编辑界面
		findDetailPage: function () {
			this.selectStock = Object.assign({}, this.row)
			this.$api.coreStockDet.GetListByStockId(this.selectStock ).then((res) => { 
				this.detailList= res.data
				this.timer = new Date().getTime(); 
			}) 
		},
		editRow:function(scope ,b){  
			this.pkLessDialogVisible=true;
			this.pkLessForm.id=scope.row.id; 
			this.timer = new Date().getTime(); 
		},
		handleClose: function () {
            this.$emit('stockViewHidden', {})
        },
		handleClosePkLess: function () {
           	this.pkLessDialogVisible=false;
        },
		 
		// 时间格式化
      	dateFormatDay: function (row, column, cellValue, index){
          	return formatDay(row[column.property])
      	},
		selectionFormat: function (row, column, cellValue, index){
          	return dictsFormat("storeStatus",cellValue)
      	}
	},
	 created () {  

	 },
	mounted(){
		
	 }
}
</script>
